package com.lizhangjun.nim.constant;


import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.UUID;

/**
 * 消息体
 * @author lizhangjun
 */
@Data
public class IMessage
{
    /**
     * 消息标识id
     */
    private String messageId;

    /**
     * 消息时间戳
     */
    private Long timestamp;

    /**
     * 来源用户id
     */
    private String fromUserId;

    /**
     * 目标用户id
     */
    private String toUserId;

    /**
     * 用户组id
     */
    private String toGroupId;

    /**
     * 数据类型
     */
    private Integer mineType;

    /**
     * 聊天类型
     */
    private Integer chatType;

    /**
     * 消息类型
     */
    private Integer msgType;

    /**
     * 消息标志
     */
    private String flag;

    /**
     * 消息内容
     */
    private String content;

    /**
     * 携带数据
     */
    private Object extras;

    public IMessage() {
    }

    public IMessage(Builder builder){
        this.messageId = builder.messageId;
        this.timestamp = builder.timestamp;
        this.fromUserId = builder.fromUserId;
        this.toUserId = builder.toUserId;
        this.toGroupId = builder.toGroupId;
        this.mineType = builder.mineType;
        this.chatType = builder.chatType;
        this.msgType = builder.msgType;
        this.flag = builder.flag;
        this.content = builder.content;
        this.extras = builder.extras;

    }

    public static class Builder{
        private final String messageId; //必传，默认uuid
        private final Long timestamp; //必传，默认服务器时间戳
        private final String flag; //必传，状态码
        private String fromUserId;
        private String toUserId;
        private String toGroupId;
        private Integer mineType;
        private Integer chatType;
        private Integer msgType;
        private String content;
        private Object extras;

        public Builder(String flag){
            this.messageId = UUID.randomUUID().toString();
            this.timestamp = System.currentTimeMillis();
            this.flag = flag;
        }

        public Builder fromUserId(String fromUserId){
            this.fromUserId = fromUserId;
            return this;
        }

        public Builder toUserId(String toUserId){
            this.toUserId = toUserId;
            return this;
        }

        public Builder toGroupId(String toGroupId){
            this.toGroupId = toGroupId;
            return this;
        }

        public Builder mineType(Integer mineType){
            this.mineType = mineType;
            return this;
        }

        public Builder msgType(Integer msgType){
            this.msgType = msgType;
            return this;
        }

        public Builder chatType(Integer chatType){
            this.chatType = chatType;
            return this;
        }

        public Builder content(String content){
            this.content = content;
            return this;
        }

        public Builder extras(Object extras){
            this.extras = extras;
            return this;
        }

        public IMessage build(){
            return new IMessage(this);
        }

        public String buildStr(){
            return JSON.toJSONString(new IMessage(this));

        }
    }
}
